# reference_usa2.rb
# october 22 2008
# 
# 
# looks up each combination on http://referenceusa.com/
# and records number of results (i.e. number of times a name appears in phone book)
# 
# input  = [fname, lname, county]
# output = [fname, lname, county, number]
#############################################################################

$output_number = 1
$file_name = "phone_names1"

require 'rubygems'
require 'fastercsv'
require 'fileutils'
require 'watir'
include Watir


def login
    $cookieDir = "C:\\Documents and Settings\\#{ENV['USERNAME']}\\Cookies" 
    FileUtils.rm_rf $cookieDir
    $ie = IE.new
    $ie.goto( your library website or referenceuse.com)
    $ie.text_field( :id, 'LibraryCard' ).set( 'a card number' )
    $ie.text_field( :id, 'PIN' ).set( 'a password' )
    $ie.form(:id, 'LoginForm').submit
    sleep 1
    $ie.goto( 'http://www.referenceusa.com/rd/rd2.asp?rusa=1')
    $ie.checkbox( :name, 'last_name' ).set
    $ie.checkbox( :name, 'first_name' ).set
    $ie.checkbox( :name, 'county' ).set
    $ie.links[8].click
end

def plug_in_name
    $ie.frame( :name, 'bottom' ).text_field( :name, 'lastname' ).set( "#{$lname}" )
    $ie.frame( :name, 'bottom' ).text_field( :name, 'firstname' ).set( "#{$fname}" )
    $ie.frame( :name, 'bottom' ).select_list( :name, 'getCountyStates' ).select( 'FLORIDA' )
    $ie.frame( :name, 'bottom' ).text_field( :name, 'countyname' ).set( "#{$county}" )
    $ie.frame( :name, 'bottom' ).links[2].click
    sleep 2
    wait_until{ $ie.link(:url, 'http://www.referenceusa.com/rd/fmset.asp?rusa=1&resetForm=false').exist? }
    sleep 2
end

def get_results
    html = $ie.html
    if html.match(/No records found matching criteria/)
        $number = 0
    else
        results = html.match(/Displaying 1-\d+ of (\d+)/)
        $number = results[1]
    end
    $error = 0
end

def output
   FasterCSV.open("#{$file_name}_results_#{$output_number}.csv", "a") { |result| result << [$fname, $lname, $county, $number] }
   puts "#{$j}---#{$lname}--#{$number}------#{Time.now.hour}:#{Time.now.min}:#{Time.now.sec}"
end

def sleep_random
    x=rand()*3 + 4
    sleep x
end

def error_output
    puts "-------------------------#{$stderr}--------#{Time.now}"
    puts "#{$j}--#{$lname}--#{$number}------#{Time.now.hour}:#{Time.now.min}:#{Time.now.sec}"
    puts $ERROR_INFO
    puts $ERROR_POSITION
    FasterCSV.open("#{$file_name}_error.csv", "a") { |result| result << [$fname, $lname, $county] }
    $error += 1
    if $error>=5
        exit
    end
    sleep 151
    $ie.close
    sleep 31

end

login
$j=1
error = 0
FasterCSV.foreach("#{$file_name}.csv") do |person|
	begin
        $fname       = person[0]
	    $lname       = person[1]
        $county      = person[2]
        
        sleep 4
        
        plug_in_name
        get_results
        output
        sleep_random
        $ie.goto("http://www.referenceusa.com/rd/fmset.asp?rusa=1&resetForm=false")
        
    rescue
        error_output
        login
        sleep 1
    rescue Timeout::Error
        error_output
        login
        sleep 1
    rescue Watir::Exception
        error_output
        login
        sleep 1
    end
$j+=1
end


